/* Standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include "cw_gauge_draw.h"
#include "CwGauge.h"

//-----------------------------------------------------------------------------
// установка шрифта
//-----------------------------------------------------------------------------
int cw_gauge_set_font (int font_size)
{
    char Helvetica [MAX_FONT_TAG];

    if(PfGenerateFontName("Helvetica", 0, font_size, Helvetica) == NULL) {
        perror("Unable to find font");
        return -1;
    } else {
        PgSetFont (Helvetica);
    }
    return 0;
}

//-----------------------------------------------------------------------------
// Прорисовка шкалы
//-----------------------------------------------------------------------------
void cw_gauge_scale_draw (PtWidget_t *widget, PhRect_t *r)  
{
	CwGaugeWidget_t		*gauge = ( CwGaugeWidget_t * ) widget;
	PhRect_t    		rect;
	PhPoint_t			p1, p2;
	int					width;
    char 				str [32];
	zone_t				*zone;
    int					i;
   
	width = r->lr.x - r->ul.x;

	PgSetStrokeColor (Pg_BLACK);
	p1.x = r->ul.x + width/2; 
	p1.y = r->ul.y;
	p2.x = p1.x;
	p2.y = r->lr.y;
	PgDrawLine (&p1, &p2);
	
	p1.x = 0;
	p1.y = gauge->n_subdiv * gauge->n_div;
	rect = *r;
	rect.ul.x = r->ul.x + width/2 - 10;
	rect.lr.x = r->ul.x + width/2 + 10;
	PgDrawGrid (&rect, &p1);
	
	p1.x = 0;
	p1.y = gauge->n_div;
	rect = *r;
	rect.ul.x = r->ul.x + width/2 - 20;
	rect.lr.x = r->ul.x + width/2 + 20;
	PgDrawGrid (&rect, &p1);

	p1.x = r->ul.x + 1;		
	p2.x = r->lr.x - 1;
	PgSetStrokeDash ("\1", 1, 0x10000);
	for (i=0; i< gauge->n_zone; i++) {
		zone = &gauge->zone [i];
		PgSetStrokeColor (zone->color);
		p1.y = p2.y = r->lr.y - (r->lr.y - r->ul.y) * (zone->min - gauge->min)/(gauge->max - gauge->min) - 1; 
		PgDrawLine (&p1, &p2);
		p1.y = p2.y = r->lr.y - (r->lr.y - r->ul.y) * (zone->max - gauge->min)/(gauge->max - gauge->min) + 1; 
		PgDrawLine (&p1, &p2);
	}
	PgSetStrokeDash (NULL, 0, 0);

    cw_gauge_set_font (10);
	PgSetTextColor (Pg_BLACK);
	for (i = 1; i < gauge->n_div; i++) {
		p1.x = r->ul.x + width/2 + 5; 
		p1.y = r->lr.y - (r->lr.y - r->ul.y) * i/gauge->n_div;
		sprintf (str, gauge->str_fmt, gauge->min + (gauge->max - gauge->min) * (double)i / (double)gauge->n_div);
		PgDrawText (str, strlen (str), &p1, Pg_TEXT_BOTTOM |Pg_TEXT_LEFT);
	}
}

//-----------------------------------------------------------------------------
// Прорисовка индикатора
//-----------------------------------------------------------------------------
void cw_gauge_pos_draw (PtWidget_t *widget, PhRect_t *r)  
{
	CwGaugeWidget_t		*gauge = ( CwGaugeWidget_t * ) widget;
	PhRect_t    		rect;
	zone_t				*zone = NULL;
	int					i;

	if (gauge->n_zone == 0) return;
	for (i=0; i< gauge->n_zone; i++) {
		zone = &gauge->zone [i];
		if ((gauge->pos >= zone->min)&& (gauge->pos < zone->max)){
			break;
		}
	}
	if ((zone)&& (gauge->pos >= zone->min)) {
		PgSetFillColor (zone->color);
		rect = *r;
		rect.ul.y += (r->lr.y - r->ul.y) * (gauge->max - gauge->pos)/(gauge->max - gauge->min);
		PgDrawRect (&rect, Pg_DRAW_FILL_STROKE);	
	}
}